/******************** (C) COPYRIGHT 2014 SONiX *******************************
* COMPANY:			SONiX
* DATE:					2014/01
* AUTHOR:				SA1
* IC:						SN32F288
* DESCRIPTION:	
*____________________________________________________________________________
* REVISION	Date				User		Description
*
*____________________________________________________________________________
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS TIME TO MARKET.
* SONiX SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL 
* DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH SOFTWARE
* AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN 
* IN CONNECTION WITH THEIR PRODUCTS.
*****************************************************************************/

/*_____ I N C L U D E S ____________________________________________________*/
#include	"SN32F280.h"
#include	"SN32F200_Def.h"
#include  "CW2017_Driver.h"
#include  "..\Driver\I2C.h"

/*_____ D E C L A R A T I O N S ____________________________________________*/


/*_____ D E F I N I T I O N S ______________________________________________*/

/*_____ M A C R O S ________________________________________________________*/


/*_____ F U N C T I O N S __________________________________________________*/


//****************************Micro*******************************/
#define	READ_CW2017				0xc7
#define	WRITE_CW2017			0xc6

/*****************************************************************************
* Function		: cw_read
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
uint8_t cw_read(unsigned char point_reg,unsigned char *r_pdata)
{
	uint8_t ack;
	unsigned char i;
	I2C_Start();
	WT_Byte(WRITE_CW2017);
	ack = 1;
	for(i = 0; i < 100; i++)
	{
		ack = I2C_CACK();
		if(!ack)
		{
			break;
		}
		if(i == 99)
		{
			I2C_Stop();
			return 1;
		}
	}
	WT_Byte(point_reg);
	ack = 1;
	for(i = 0; i < 100; i++)
	{
		ack = I2C_CACK();
		if(!ack)
		{
			break;
		}
		if(i == 99)
		{
			I2C_Stop();
			return 1;
		}
	}
	I2C_Start();
	WT_Byte(READ_CW2017);
	ack = 1;
	for(i = 0; i < 100; i++)
	{
		ack=I2C_CACK();
		if(!ack)
		{
			break;
		}
		if(i == 99)
		{
			I2C_Stop();
			return 1;
		}
	}

	*r_pdata = RD_Byte();
	I2C_MNACK();
	I2C_Stop();
	return 0;
}

/*****************************************************************************
* Function		: cw_write
* Description	: 
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
uint8_t cw_write(unsigned char point_reg,unsigned char *w_pdata)
{
	uint8_t ack;
	unsigned char i;
	I2C_Start();
	WT_Byte(WRITE_CW2017);
	ack = 1;
	for(i = 0; i < 100; i++)
	{
		ack = I2C_CACK();
		if(!ack)
		{
			break;
		}
		if(i == 99)
		{
			I2C_Stop();
			return 1;
		}
	}
	WT_Byte(point_reg);
	ack = 1;
	for(i = 0; i < 100; i++)
	{
		ack = I2C_CACK();
		if(!ack)
		{
			break;
		}
		if(i == 99)
		{
			I2C_Stop();
			return 1;
		}
	}
	WT_Byte(*w_pdata);
	ack = 1;
	for(i = 0; i < 100; i++)
	{
		ack = I2C_CACK();
		if(!ack)
		{
			break;
		}
		if(i == 99)
		{
			I2C_Stop();
			return 1;
		}
	}
	I2C_Stop();
	return 0;
}
